import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { environment } from '../../environments/environment';
import { BaseService } from './base.service';
import { HttpSerice } from '../service/https.sercice';
import { HttpPcSerice } from '../service/https.pc.sercice ';

declare var THREE: any;
declare var jumpRoom: any;
declare var jumpUserInfo: any;
declare var jumpRoomNew: any;
@Injectable({
    providedIn: 'root'
})
export class UtilsService {
    public isWitch: boolean = false;
    constructor(private BaseService: BaseService, private http: HttpSerice, private httppcService: HttpPcSerice,) { }
    public MsgSub = new Subject<any>();//刷新查看榜单 
    public thisMsgSub = new Subject<any>();//当前用户主播信息
    public add(name: string, value: any): void {
        var storage: any = window.localStorage;
        storage.setItem(name, value);
    }
    public update(name: string, value: any): void {
        var storage = window.localStorage;
        storage.name = value;
    }
    public select(name: string): string {
        var storage = window.localStorage;
        return storage.getItem(name);
    }
    public delete(name: string): void {
        var storage = window.localStorage;
        storage.removeItem(name);
    }
    public deleteAll(): void {
        var storage = window.localStorage;
        storage.clear();
    }
    /**
   * 获取当前手机系统
   */
    public getBrowserVersion(): boolean {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        if (isAndroid) {
            return true;
        } else {
            return false;
        }
    }
    /**
* 查询基本信息
* @param suc 
* @param err 
*/
    public getUserInfo(suc: any, err: any) {
        var url = this.httppcService.baseUrl + "users/personal";
        return this.httppcService.httpGet(url, suc, err)
    }

    public IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = [
            "Android",
            "iPhone",
            "SymbianOS",
            "Windows Phone",
            "iPad",
            "iPod"
        ];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * 根据UID获取靓号
    * @param suc 
    * @param uid 查询用户的UID 
    * @param err 
    */
    public getUserNumber(suc: any, uid: number, err: any) {
        var url = this.httppcService.baseUrl + "users/" + uid + "/number";
        return this.httppcService.httpGet(url, suc, err)
    }

    public gotoRoom(item: any, type, liveAnchors = []) {
        if (this.IsPC()) {
            if (item.isMysteryMan) {
                return;
            }
            if (type == 'anchor') {
                if (item.liveStatus == true) {
                    if (item.uid == this.BaseService.getRoomid()) {
                        this.BaseService.showToast('你已在当前直播间', 1500);
                        return;
                    }
                    this.getUserInfo((res: any) => {
                        if (res.code == 200) {
                            if (res.data.status) {
                                this.BaseService.showToast("当前正在开播,不能进入别人的直播间~", 1500);
                                return;
                            } else {
                               
                                    this.getUserNumber((res: any) => {
                                        if (res.code == 200) {
                                            let node = document.createElement("a");
                                            console.log('node',node)
                                            node.target = '_newroom';
                                            node.href = "/" + res.data.number;
                                            document.body.appendChild(node);
                                            node.click();
                                            document.body.removeChild(node);
                                        }
                                    }, item.uid, (err: any) => { })
                                
                            }
                        }
                    }, (err: any) => { })
                } else {
                    let node = document.createElement("a");
                    node.target = '_space';
                    node.href = "/personinfo/" + item.uid.toString().slice(2, 4) + item.uid * 3;
                    document.body.appendChild(node);
                    node.click();
                    document.body.removeChild(node);
                }
            } else {
                let node = document.createElement("a");
                node.target = '_space';
                node.href = "/personinfo/" + item.uid.toString().slice(2, 4) + item.uid * 3;
                document.body.appendChild(node);
                node.click();
                document.body.removeChild(node);
            }
        } else {
            if (item.isMysteryMan) {
                return;
            }
            if (!this.getBrowserVersion()) {
                if (type == 'anchor') {
                    if (item.uid == this.BaseService.getRoomid()) {
                        this.BaseService.showToast('你已在当前直播间', 1500);
                        return;
                    }
                    if (item.liveStatus == true) {
                        if (this.BaseService.getVersionCode() > environment.versionCode) {
                            let url = "banbanlive://jumproom?uid=" + item.uid + "&liveAnchors=" + window.btoa(encodeURIComponent(JSON.stringify(liveAnchors))) + "&activityName=活动&scene=yinghua&recommend=0";
                            window.location.href = url;
                        } else {
                            let url = "banbanlive://jumproom?uid=" + item.uid;
                            window.location.href = url;
                        }
                    } else {
                        let url = "banbanlive://jumpuserdetail?uid=" + item.uid;
                        window.location.href = url;
                    }
                } else {
                    let url = "banbanlive://jumpuserdetail?uid=" + item.uid;
                    window.location.href = url;
                }

            } else {
                if (type == 'anchor') {
                    if (item.uid == this.BaseService.getRoomid()) {
                        this.BaseService.showToast('你已在当前直播间', 1500);
                        return;
                    }
                    if (item.liveStatus == true) {
                        if (this.BaseService.getVersionCode() > environment.versionCode) {
                            jumpRoomNew(item.uid, item.nickname, item.livImage, environment.liveUrl + item.uid, JSON.stringify(liveAnchors), "活动", "yinghua", "0");
                        } else {
                            jumpRoom(item.uid, item.nickname, item.livimage, environment.liveUrl + item.uid);
                        }
                    } else {
                        jumpUserInfo(item.uid)
                    }
                } else {
                    jumpUserInfo(item.uid)
                }
            }
        }
    }
}



